package com.feicheng.house.service.impl;

import com.feicheng.house.entity.Deposit;
import com.feicheng.house.entity.Record;
import com.feicheng.house.entity.RolePermission;
import com.feicheng.house.entity.User;
import com.feicheng.house.entity.UserRole;
import com.feicheng.house.exception.SystemException;
import com.feicheng.house.mapper.DepositMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IDepositService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class DepositServiceImpl extends ServiceImpl<DepositMapper, Deposit> implements IDepositService {
		
		@Override
		public ResultInfo getDepositInfo(Integer depositId) {
			return ResultInfo.success(this.getById(depositId));
		}
		
		@Override
		public ResultInfo addDepositInfo(Deposit deposit) {
			if (null == deposit.getClientId()) {
				throw new SystemException("请输入客户编号！");
			};
			if (null == deposit.getDeposit()) {
				throw new SystemException("请输入押金金额！");
			};
			if (null == deposit.getDepositState()) {
				throw new SystemException("请输入押金的状态！");
			};
			this.save(deposit);
			return ResultInfo.success();
		}

		@Override
		public ResultInfo editDepositInfo(Deposit deposit) {
			if (deposit.getDepositId() == null || deposit.getDepositId() == 0) {
				throw new SystemException("请输入押金编号！");
				}
			QueryWrapper<Deposit> wrapper = new QueryWrapper<Deposit>();
			wrapper.eq("deposit_id", deposit.getDepositId());
			Deposit deposit1 = this.getOne(wrapper);
			deposit1.setDepositId(deposit1.getDepositId());
			this.update(deposit, wrapper);
			return ResultInfo.success() ;
		}
		
		@Override
		public ResultInfo pagesInfo(Integer pageSize , Integer currentPage , String searchKey) {
			QueryWrapper<Deposit> depositQueryWrapper = new QueryWrapper<Deposit>();
			depositQueryWrapper.eq("delete_flag", true);
			if (StringUtils.hasLength(searchKey)) {
				depositQueryWrapper.like("deposit_id", searchKey)
				.or().like("client_id", searchKey)
				.or().like("deposit_state", searchKey)
				.or().like("deposit", searchKey);
			}
			IPage<Deposit> pageInfo = new Page<Deposit>(currentPage , pageSize);
			return ResultInfo.success(this.page(pageInfo,depositQueryWrapper));
		}
}
